from django.shortcuts import get_object_or_404

from apps.location.models import RoomOperatingHours, RoomClosedDate, RoomAuthorization, RoomPrice, LocationTierSetting
from datetime import datetime

from apps.users.models import Provider


def save_room_operating_hours(room, operating_hours):
    query = list()
    for hours in operating_hours:
        hours['room'] = room
        query.append(RoomOperatingHours(**hours))
    RoomOperatingHours.objects.bulk_create(query)


def save_room_closed_dates(room, closed_dates):
    query = list()
    for closed_date in closed_dates:
        closed_date['room'] = room
        closed_date['date'] = datetime.fromtimestamp(closed_date.get('date'))
        query.append(RoomClosedDate(**closed_date))

    RoomClosedDate.objects.bulk_create(query)


def save_room_authorization(room, room_authorization):
    query = list()
    for authorization in room_authorization:
        authorization['room'] = room
        authorization['user'] = get_object_or_404(Provider, id=authorization['user'])
        query.append(RoomAuthorization(**authorization))
    RoomAuthorization.objects.bulk_create(query)


def save_room_price(room, room_prices):
    query = list()
    for price in room_prices:
        price['room'] = room
        price['tier'] = get_object_or_404(LocationTierSetting, id=int(price['tier']))
        query.append(RoomPrice(**price))
    RoomPrice.objects.bulk_create(query)
